home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / util / singletonmixin.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  7KB  |  167 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4.  
  5. class SingletonException(Exception):
  6.     pass
  7.  
  8.  
  9. class Singleton(object):
  10.     
  11.     def getInstance(cls, *lstArgs):
  12.         if cls._isInstantiated():
  13.             if len(lstArgs) != 0:
  14.                 raise SingletonException, 'If no supplied args, singleton must already be instantiated, or __init__ must require no args'
  15.             
  16.         elif cls._getConstructionArgCountNotCountingSelf() > 0 and len(lstArgs) <= 0:
  17.             raise SingletonException, 'If the singleton requires __init__ args, supply them on first instantiation'
  18.         
  19.         instance = cls.__new__(cls)
  20.         instance.__init__(*lstArgs)
  21.         cls.cInstance = instance
  22.         return cls.cInstance
  23.  
  24.     getInstance = classmethod(getInstance)
  25.     
  26.     def _isInstantiated(cls):
  27.         return hasattr(cls, 'cInstance')
  28.  
  29.     _isInstantiated = classmethod(_isInstantiated)
  30.     
  31.     def _getConstructionArgCountNotCountingSelf(cls):
  32.         return cls.__init__.im_func.func_code.co_argcount - 1
  33.  
  34.     _getConstructionArgCountNotCountingSelf = classmethod(_getConstructionArgCountNotCountingSelf)
  35.     
  36.     def _forgetClassInstanceReferenceForTesting(cls):
  37.         
  38.         try:
  39.             delattr(cls, 'cInstance')
  40.         except AttributeError:
  41.             for baseClass in cls.__bases__:
  42.                 if issubclass(baseClass, Singleton):
  43.                     baseClass._forgetClassInstanceReferenceForTesting()
  44.                     continue
  45.             
  46.  
  47.  
  48.     _forgetClassInstanceReferenceForTesting = classmethod(_forgetClassInstanceReferenceForTesting)
  49.  
  50. if __name__ == '__main__':
  51.     import unittest
  52.     
  53.     class PublicInterfaceTest(unittest.TestCase):
  54.         
  55.         def testReturnsSameObject(self):
  56.             
  57.             class A((Singleton,)):
  58.                 
  59.                 def __init__(self):
  60.                     super(A, self).__init__()
  61.  
  62.  
  63.             a1 = A.getInstance()
  64.             a2 = A.getInstance()
  65.             self.assertEquals(id(a1), id(a2))
  66.  
  67.         
  68.         def testInstantiateWithMultiArgConstructor(self):
  69.             
  70.             class B((Singleton,)):
  71.                 
  72.                 def __init__(self, arg1, arg2):
  73.                     super(B, self).__init__()
  74.                     self.arg1 = arg1
  75.                     self.arg2 = arg2
  76.  
  77.  
  78.             b1 = B.getInstance('arg1 value', 'arg2 value')
  79.             b2 = B.getInstance()
  80.             self.assertEquals(b1.arg1, 'arg1 value')
  81.             self.assertEquals(b1.arg2, 'arg2 value')
  82.             self.assertEquals(id(b1), id(b2))
  83.  
  84.         
  85.         def testTryToInstantiateWithoutNeededArgs(self):
  86.             
  87.             class B((Singleton,)):
  88.                 
  89.                 def __init__(self, arg1, arg2):
  90.                     super(B, self).__init__()
  91.                     self.arg1 = arg1
  92.                     self.arg2 = arg2
  93.  
  94.  
  95.             self.assertRaises(SingletonException, B.getInstance)
  96.  
  97.         
  98.         def testTryToInstantiateWithoutGetInstance(self):
  99.             
  100.             class A((Singleton,)):
  101.                 
  102.                 def __init__(self):
  103.                     super(A, self).__init__()
  104.  
  105.  
  106.             self.assertRaises(SingletonException, A)
  107.  
  108.         
  109.         def testDontAllowNew(self):
  110.             
  111.             def instantiatedAnIllegalClass():
  112.                 
  113.                 class A((Singleton,)):
  114.                     
  115.                     def __init__(self):
  116.                         super(A, self).__init__()
  117.  
  118.                     
  119.                     def __new__(metaclass, strName, tupBases, dict):
  120.                         return super(MetaSingleton, metaclass).__new__(metaclass, strName, tupBases, dict)
  121.  
  122.  
  123.  
  124.             self.assertRaises(SingletonException, instantiatedAnIllegalClass)
  125.  
  126.         
  127.         def testDontAllowArgsAfterConstruction(self):
  128.             
  129.             class B((Singleton,)):
  130.                 
  131.                 def __init__(self, arg1, arg2):
  132.                     super(B, self).__init__()
  133.                     self.arg1 = arg1
  134.                     self.arg2 = arg2
  135.  
  136.  
  137.             b1 = B.getInstance('arg1 value', 'arg2 value')
  138.             self.assertRaises(SingletonException, B, 'arg1 value', 'arg2 value')
  139.  
  140.         
  141.         def test_forgetClassInstanceReferenceForTesting(self):
  142.             
  143.             class A((Singleton,)):
  144.                 
  145.                 def __init__(self):
  146.                     super(A, self).__init__()
  147.  
  148.  
  149.             
  150.             class B((A,)):
  151.                 
  152.                 def __init__(self):
  153.                     super(B, self).__init__()
  154.  
  155.  
  156.             a = A.getInstance()
  157.             A._forgetClassInstanceReferenceForTesting()
  158.             b = B.getInstance()
  159.             B._forgetClassInstanceReferenceForTesting()
  160.             a = A.getInstance()
  161.             B._forgetClassInstanceReferenceForTesting()
  162.             b = B.getInstance()
  163.  
  164.  
  165.     unittest.main()
  166.  
  167.